/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.HashMap;
import java.util.Map;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ScanTypeRest;
import com.sip.dbadapter.dao.domain.ScenarioRecord;
import com.sip.dbadapter.dao.interfaces.ApiDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.dbadapter.domain.registry.DomainRegistry;
import com.sip.executionadapter.manager.JerseyExecutionManager;

/**
 * @author Dhanushka Ariyasingha.
 */
public class SAMTestSuitExecutor extends BaseExecutor implements Executor {

    public CommunicationMessage execute(final CommunicationMessage message) throws Exception {

        final String messageString = message.getMessageBody();
        final String jsonString = (String) message.getObject();

        String apiId = CommonUtils.find(messageString, "API_ID");

        String pathParams = null;
        if (messageString.contains("PATH_PARAM")) {
            pathParams = CommonUtils.find(messageString, "PATH_PARAM");
        }

        String queryParams = null;
        if (messageString.contains("QUERY_PARAM")) {
            queryParams = CommonUtils.find(messageString, "QUERY_PARAM");
        }

        String resourceBase = null;
        if (messageString.contains("RESOURCE_BASE")) {
            resourceBase = messageString.replaceFirst(".*RESOURCE_BASE:", "");
        }

        String headerFields = null;
        if (messageString.contains("HTTP_HEADER")) {
            headerFields = CommonUtils.find(messageString, "HTTP_HEADER");
        }

        final ScanTypeRest scanTypeRestDomainObj = new ScanTypeRest();
        scanTypeRestDomainObj.setResourceBase(resourceBase.trim());

        final ApiDao apiDao = (ApiDao) DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
        final ApiDefinition apiDefinition = (ApiDefinition) apiDao.findById(Long.parseLong(apiId));

        final ApiDefinition apiDefinitionDomainObj = new ApiDefinition();
        apiDefinitionDomainObj.setApiId(Long.parseLong(apiId));

        String resourceUrl = "";

        if (pathParams == null) { // resource string without pathparams
            resourceUrl = apiDefinition.getPackageHierachy().trim();
        } else { // resource string with pathparams
            resourceUrl = pathParams;
        }

        if (queryParams != null) {
            resourceUrl = resourceUrl + queryParams;
        }

        apiDefinitionDomainObj.setPackageHierachy(resourceUrl);

        final ScenarioRecord scenarioRecordDomainObj = new ScenarioRecord();
        scenarioRecordDomainObj.setConsumeValue(jsonString);

        Map<String, String> httpHeaders = new HashMap<String, String>();

        if (null != headerFields)
            httpHeaders = createHeaderMap(headerFields);

        final Map<String, Object> domainMap = new HashMap<String, Object>();

        domainMap.put(DomainRegistry.API_DEFINITION, apiDefinitionDomainObj);
        domainMap.put(DomainRegistry.SCENARIO_RECORD, scenarioRecordDomainObj);
        domainMap.put(DomainRegistry.SCAN_TYPE_REST, scanTypeRestDomainObj);
        domainMap.put(DomainRegistry.DOMAIN_RELATED_GENERIC_INFO, httpHeaders);

        final JerseyExecutionManager jerseyManager = JerseyExecutionManager.createInstance();
        jerseyManager.invoke(domainMap);

        final ScenarioRecord returnedScenarioRecordDomainObj = (ScenarioRecord) domainMap
                .get(DomainRegistry.SCENARIO_RECORD);
        message.setObject(returnedScenarioRecordDomainObj.getResult());

        return message;
    }

    private Map<String, String> createHeaderMap(String headerFields) {

        String[] headers = headerFields.split(",");

        Map<String, String> httpHeaders = new HashMap<String, String>();
        for (String h : headers) {
            String[] keyValues = h.split("&");
            System.out.println(keyValues[0]);
            httpHeaders.put(keyValues[0], keyValues[1]);
        }
        return httpHeaders;
    }

}
